package com.sskj.camera.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sskj.camera.domain.Camera;
import com.sskj.camera.mapper.CameraMapper;
import com.sskj.camera.service.ICameraService;
import lombok.Data;
import org.springframework.stereotype.Service;

@Service
@Data
public class CameraService extends ServiceImpl<CameraMapper, Camera> implements ICameraService {

    @Override
    public String buildRTSPUrl(Camera camera) {
        if (camera == null) {
            return null;
        }

        String ip = camera.getIp();
        Integer rtspPort = camera.getRtspPort();
        String username = camera.getRtspUsername();
        String password = camera.getRtspPassword();
        String streamKey = camera.getStreamKey();

        // 基础校验
        if (ip == null || ip.trim().isEmpty() || rtspPort == null || streamKey == null || streamKey.trim().isEmpty()) {
            return null;
        }

        // 构建认证信息（如果用户名存在）
        StringBuilder sb = new StringBuilder();
        sb.append("rtsp://");

        if (username != null && !username.isEmpty()) {
            sb.append(username);
            if (password != null && !password.isEmpty()) {
                sb.append(":").append(password);
            }
            sb.append("@");
        }

        // IP 和端口
        sb.append(ip).append(":").append(rtspPort);

        // 添加 streamKey（SRS 通常格式为 /app/streamKey）
        // 假设 SRS 配置的应用名为 live，可根据实际配置调整
        sb.append("/live/").append(streamKey.trim());

        return sb.toString();
    }
}
